﻿using EmailManagementSoftware.Presenter;
using EmailManagementSoftware.View.Interfaces;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using EmailManagementSoftware.Model;

namespace EmailManagementSoftware
{
    public partial class EmailMessageView : Form, IEmailMessageView
    {
        public AppPresenter _AppPresenter { get; set; } 


        private EmailMessageView()
        {
            InitializeComponent();
        }

        public EmailMessageView(AppPresenter appPresenter): this()
        {
            this._AppPresenter = appPresenter;
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnAddAttachement_Click(sender,e);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnRemove_Click(sender, e);
            if (lbxAttachements.Items.Count == 0)
                btnRemove.Enabled = false;
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnClearAll_Click(sender, e);
            btnClearAll.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnSaveEmailMessage_Click(sender, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnCancelEmailMessage_Click(sender, e);
        }

        private void lbxAttachements_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemove.Enabled = (lbxAttachements.SelectedIndex != -1);
        }


        public string _WindowTitle
        {
            get
            {
                return Text;
            }
            set
            {
                Text = value;
            }
        }

        public string _Subject
        {
            get
            {
                return tbxSubject.Text;
            }
            set
            {
                tbxSubject.Text = value;
            }
        }

        public string _Recipient
        {
            get
            {
                return tbxTo.Text;
            }
            set
            {
                tbxTo.Text = value;
            }
        }

        public string _Body
        {
            get
            {
                return richTextBox.Text;
            }
            set
            {
                richTextBox.Text = value;
            }
        }

        public int _SelectedAttachementIndex
        {
            get { return lbxAttachements.SelectedIndex; }
        }

        public Dictionary<string, string> _ShortPathFullPathPairs
        {
            get;
            set;
        } = new Dictionary<string, string>();

        public string _SelectedAttachement
        {
            get
            {
                { return lbxAttachements.SelectedItem as string; }
            }
        }

        public void addStringAttachement(string stringAttachement)
        {
            lbxAttachements.Items.Add(stringAttachement);
        }

        public void removeStringAttachement(int index)
        {
            lbxAttachements.Items.RemoveAt(index);
        }

        public void removeAllStringAttachements()
        {
            lbxAttachements.Items.Clear();
        }

        private void lbxAttachements_Leave(object sender, EventArgs e)
        {
            lbxAttachements.ClearSelected();
        }

        public void updateStateOfListBox()
        {
            btnClearAll.Enabled = (lbxAttachements.Items.Count>0);
            btnRemove.Enabled = (lbxAttachements.SelectedIndex != -1);
        }
    }
}
